<?php
    require_once("include/common.php");

    $user = $_GET['id'] ?? '' ;
?>

<!DOCTYPE html>
<html lang="en" ng-app="deviceApp">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">

<title>表计列表</title>

    <?php
        require_once("pages/resources.php");
    ?>
 
<!--     <link href="/meter/resources/bootstrap-3.3.6/css/bootstrap.min.css" rel="stylesheet"> -->

    <script src="/meter/resources/jquery/jquery-2.0.3.js"></script>
    
    <script src="/meter/resources/angular-1.5.5/angular.js"></script>
    <script src="/meter/resources/js-base-util.js"></script>
    <script type="text/javascript" src="/meter/resources/layer/layer.js"></script>
    <script type="text/javascript" src="/meter/resources/js/tags.min.js"></script> 
 
</head>
<body  id="deviceCtrl" ng-controller="deviceCtrl">

  
    <!-- Page container -->
    <div class="page-container">

                 

                        <!--   -->
                  
                            <!-- Form begin -->
                            <form class="form-horizontal" role="form" ng-submit="save();">
                                <div class="panel panel-default">
                                    
                                    <div class="panel-body">
                                   
                                        <div class="form-group  ">
                                            <label class="col-sm-2 control-label"> ID :</label>
                                            <div class="col-sm-4">
                                                <input type="text" class="form-control" id="_INPUT_USERID" ng-model="user._USERID"  
                                                 <?php 
                                                 if( $user ){
                                                    print('readonly="readonly"');   
                                                 }
                                                 
                                                 ?>
                                                 >  
                                            </div>
                                            
                                            
                                        
                                        </div>
                                        <div class="form-group  ">
                                            <label class="col-sm-2 control-label"> NAME :</label>
                                                <div class="col-sm-4">
                                                    <input type="text" class="form-control" id="_INPUT_NAME" ng-model="user._NAME" >  
                                                </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label"> Enable:  </label>
                                            <div class="col-sm-4">
                                            
                                                   <input type="checkbox"   ng-model="enable"> 
                                                 
                                            
                                            </div>
                                        </div>  

                                        <div class="form-group">

                                                <label class="col-sm-2 control-label">Tags:</label>
                                                <div class="col-sm-10">
                                                    <input type="text" id="tags" class="tags"  value="">
                                                </div>

                                                
                                        </div>  

                                               
                                        <div class="form-group  ">
                                            <label class="col-sm-2 control-label"> Updated :</label>
                                            <div class="col-sm-4">
                                                <input type="text" class="form-control" value="{{user._UPDATED}}" readonly="">  
                                            </div>
                                            
                                            <label class="col-sm-2 control-label"> Synced :</label>
                                            <div class="col-sm-4">
                                                <input type="text" class="form-control" value="{{user._SYNC}}" readonly="">  
                                            </div>
                                 
                                        </div>   

                                           

                                        <div class="form-actions text-right">
                                            <input type="submit" value="Save"   class="btn btn-primary">
                                        </div>

                                    </div>
                                </div>
                            </form>
                                     
                        <!-- form end -->
                        </div>
              
     

</body>
<script type="text/javascript">

var app = angular.module("deviceApp", [], function ($httpProvider) {  
    $httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8";  
    $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';  
    var param = function (obj) {  
        var query = "", name, value, fullSubName, subName, subValue, innerObj, i;  
        for (name in obj) {  
            value = obj[name];  
            if (value instanceof Array) {  
                for (i = 0; i < value.length; ++i) {  
                    subValue = value[i];  
                    fullSubName = name + "[" + i + "]";  
                    innerObj = {};  
                    innerObj[fullSubName] = subValue;  
                    query += param(innerObj) + "&";  
                }  
            } else if (value instanceof Object) {  
                for (subName in value) {  
                    subValue = value[subName];  
                    fullSubName = name + "[" + subName + "]";  
                    innerObj = {};  
                    innerObj[fullSubName] = subValue;  
                    query += param(innerObj) + "&";  
                }  
            } else if (value !== undefined && value !== null) {  
                query += encodeURIComponent(name) + "=" + encodeURIComponent(value) + "&";  
            }  
        }  
        return query.length ? query.substr(0, query.length - 1) : query;  
    };  
    $httpProvider.defaults.transformRequest = [function (data) {  
        return angular.isObject(data) && String(data) !== "[object File]" ? param(data) : data;  
    }];  
});  
 
var input_user = '<?=$user?>';


    app.controller('deviceCtrl',
        function ($scope, $http, $interval , $timeout) {

            $scope.user = {};     
    
            $scope.getUser  = function (user) {
                if( !user )
                    return ;
                var url = '/meter/rest/disp.php?m=user'  ;
                // var url = "/rest/api/data/devRT?" ;

                url = _appendUrlList( url, "user" , user );
                     
                $http.get( url )
                    .success(
                        function(response) 
                        {
                            response_status( 'get user :' + user  ,  response );
                            $scope.user = response.result;
                            $scope.enable = $scope.user._ENABLE? true:false;

                            $('#_INPUT_USERID').attr('readonly','readonly');

                            if( $scope.user._TAGS ) {

                              
                                $timeout((function() {
                                        bindTags( $scope.user._TAGS );
                                }), 0);
                            }
                        }
                );
            };

            $scope.checkUser = function( uid , exist , noexist ){
                var url = '/meter/rest/disp.php?m=usercheck'  ;
                // var url = "/rest/api/data/devRT?" ;

                url = _appendUrlList( url, "user" , uid );
                     
                $http.get( url )
                    .success(
                        function(response) 
                        {
                            response_status( 'checkuser :' + uid  ,  response );
                            var find = response.result;
                            if( find == 1 ){
                                exist(uid);
                            }else if(find == 0 ){
                                noexist(uid);
                            }else {
                                // ???
                                alert( "unkown result " + find 
                                    );
                            }
                        }
                );
         
            }

            $scope.save = function () {
                if (! $scope.user._USERID ) {
                    layer.tips('UserId 不能为空' , '#_INPUT_USERID' ,{
                            tips: 4
                        });
                    return ;
                }

                if (! $scope.user._NAME ) {
                    layer.tips(' NAME 不能为空' , '#_INPUT_NAME' ,{
                           // tips: 4
                        });
                    return ;
                }

                


                var readonly = $('#_INPUT_USERID').attr('readonly');
                if( readonly ) {
                    $scope.saveRaw();
                }else {
                    $scope.checkUser( $scope.user._USERID , function(u){
                        
                        layer.tips('UserId ' + u + '已经存在了' , '#_INPUT_USERID' ,{
                            tips: 4
                        });
                    } , 
                    function(u){
                        $scope.saveRaw();
                    });
                }
            }  
          
            $scope.saveRaw = function () {
                 
                var stags = $('#tags').val() ;

                var url = '/meter/rest/disp.php?m=usersave'  ;
                // var url = "/rest/api/data/devRT?" ;
                var uid = $scope.user._USERID ? $scope.user._USERID : '';
                var name = $scope.user._NAME ? $scope.user._NAME : '';
                var enable = $scope.user.enable ? $scope.user.enable : '1';
              
                $http.post( url ,{
                        "name" : name , 
                        "user" : uid , 
                        "enable" : enable  ? 1 : 0 , 
                        "tags" : stags 
                    } )
                    .success(
                        function (response) {
                            response_status( 'save user :' + uid  ,  response ); 
                            if (response.status != 0) {
                                return;
                            }
                           
                            $scope.getUser(uid);

                            
                        }
                    );
            };

            $scope.getUser(input_user);

            //   

        });
    
    function bindTags(value){
        var tags = $('#tags');
        tags.importTags(value);

        // var list = value.split(",");
        // for( var val in list)
        //     tags.addTag( val );
    }
     $('.tags').tagsInput({width:'100%'});


    function selectFile(){
       $('#uploadFile').click();
    }



    function reloadUnit(  ){
        var ctrl = angular.element('#deviceCtrl');
        var $scope= ctrl.scope();
 
        $scope.getUnit(unitId);
    }

 
    function response_status( command , result ){
        if(result.status == 0 ){
            $('#responseId').removeClass('label-warning');
            $('#responseId').addClass('label-success');
            $('#responseId').text( 'success! ' + command);
        }else {
            $('#responseId').removeClass('label-success');
            $('#responseId').addClass('label-warning');
            
            $('#responseId').text( 'Error : ' + result.status + ' ' + result.message + '\n' + command );
        }
    }
    
     

</script>
</html>
